package movEat.GUI;

import movEat.Definitions.Decade;
import movEat.Definitions.Genre;
import movEat.Definitions.MovieDB;
import movEat.Definitions.MovieLink;
import movEat.Definitions.Pair;
import movEat.Definitions.Rating;
import movEat.Definitions.TVshowDB;
import movEat.Definitions.actorDB;
import movEat.GUI.SWTUtil;
import movEat.GUI.Ordering.AppetizerOrderDetails;
import movEat.GUI.Ordering.DrinksOrderDetails;
import movEat.GUI.Ordering.MainCourseOrderDetails;
import movEat.GUI.Ordering.OrderDetails;
import movEat.Generic.IError;
import movEat.Generic.IProgress;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Font;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

import movEat.IGui.*;
import movEat.IGui.OrderDetails.IOrderDetails;
import movEat.IKernel.*;
import movEat.Kernel.Import.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
//import javazoom.jl.player.Player;
import java.util.Map;

public class GUI implements IGui {

	private int MAX_NUMBER_OF_RESULTS = 1000;
	static int ACTORS_FOR_EDIT = 0;
	static int MOVIES_FOR_EDIT = 1;
	static int TV_FOR_EDIT = 2;
	static int ACTORS_FOR_ADDCON = 3;
	static int MOVIES_FOR_ADDCON = 4;

	private static IKernel ker = null;

	private Label totalOrder;
	private int appOrderSum = 0;
	private int mainCourseOrderSum = 0;
	private int drinksOrderSum = 0;
	private int totalOrderSum = 0;
	private int prevAppOrderSum = 0;
	private int prevMainOrderSum = 0;
	private int prevDrinkOrderSum = 0;

	private Text logMessage;
	private Button orderButton;
	static Shell shell = null;

	// the second shell that is open right now
	static Shell subShell = null;

	static Image mainBackground = new Image(SWTUtil.getDisplay(),"resources/bg1.jpg");
	private CTabFolder tabFolder;

	// Progress bar
	ProgressBar bar;
	Label updateStatus;
	Button closePBshell;

	private Label Description = null;
	private OrderDetails orderDetails = null;

	/* EDIT */

	// Edit Movies
	// Shell editDeleteMovieShell = null;
	private MovieDB movieToFind_forEdit = null;
	private Map<String, MovieDB> moviesForEdit = null;
	private Map<Integer, MovieDB> moviesFoundForEdit = null;
	private String[] moviesForEditNames = null;
	private MovieDB editedMovie = null;

	// Edit Movies - step one
	private MovieParametersComposite editMovie_stepOne;
	private Button findMoviesButton = null;

	// edit movies - step two
	private MovieDB selectedMovie = null;
	private Integer selectedMovie_id;
	private Composite editMovie_stepTwo = null;
	private Combo moviesFoundCombo = null;
	private Button selectMovieButton = null;

	// edit movies - step Three
	private MovieParametersComposite editMovie_stepThree;
	private Button editMovieButton = null;
	private Button deleteMovieButton = null;

	// Edit Actors

	// Shell editDeleteActorShell = null;
	private actorDB actorToFind_forEdit = null;
	private Map<Integer, String> actorsFoundForEdit = null;
	private String[] actorsForEditNames = null;

	// edit actors - step one
	private ActorParameters editActor_stepOne;
	private Button findActorButton = null;

	// edit actors - step two

	private Composite editActor_stepTwo = null;
	private Combo actorsFoundCombo = null;
	private Button selectActorButton = null;
	private String selectedActor = null;
	private Integer selectedActor_id;

	// edit actors - step three

	private Text editActorName_edit;
	private Button editActorButton;
	private Button deleteActorButton;
	private String editedActor = null;

	// edit TV Show
	// Shell editDeleteTvShowShell = null;
	private Map<Integer, TVshowDB> tvShowsFoundForEdit;
	private String[] tvShowsForEditNames;
	private HashMap<String, TVshowDB> tvShowsForEdit;
	private TVshowDB tvShowToFind_forEdit = null;

	// edit TV Show - step one
	private TvShowParametersComposite editTvShow_stepOne;
	private Button findTvShowButton;

	// edit TV Show - step two

	private Combo tvShowsFoundCombo;
	private Composite editTvShow_stepTwo;
	private Button selectTvShowButton;
	private TVshowDB selectedTvShow;
	private Integer selectedTvShow_id;

	// edit TV Show - step three
	private TvShowParametersComposite editTvShow_stepThree;
	private Button editTvShowButton;
	private TVshowDB editedTvShow;
	private Button deleteTvShowButton;

	/*** ORDER ***/

	private Label totalBillLabel;
	private int totalBillSum=0;
	private Button payButton;

	/* appetizer */
	private Button appButtonCheck = null;
	private Button firstDecadeRadio = null;
	private Button secondDecadeRadio = null;
	private Button thirdDecadeRadio = null;
	private Scale appPrice = null;
	private Text appPriceValue = null;

	/* Main Course */
	private Button mainCourseButtonCheck = null;
	private Button byActor = null;
	private Button byOtherParameters = null;
	private FindActorForMainCourse findActorForMainCourse = null;
	private Label selectedActorForMainCourse = null;
	private Button locateActor = null;
	// private Button actorsListButton = null;
	private Button FusionButton = null;
	// private Combo actorsCombo = null;
	private Combo countriesCombo = null;
	private Combo genresCombo = null;
	private Scale mainCoursePrice = null;
	private Text mainPriceValue = null;

	/* drinks */
	private Button drinksButtonCheck = null;
	private Button alcoholButton = null;
	private Button softDrinksButton = null;
	private Button cofeeButton = null;
	private Scale drinksPrice = null;
	private Text drinksPriceValue = null;

	/* result shell */
	// private Shell orderResultShell;
	private Button eatButton;
	private Button backButton;
	private Combo McCombo;
	private Combo appCombo;
	private Combo desCombo;
	private Combo drinkCombo;

	private ArrayList<Pair<String, Integer>> appResultList = null;
	private ArrayList<Pair<String, Integer>> MCResultList = null;
	private ArrayList<Pair<String, Integer>> drinkResultList = null;
	private ArrayList<Pair<String, Integer>> desertResultList = null;

	private boolean appQuerySignal=false;
	private boolean MCQuerySignal=false;
	private boolean drinkQuerySignal=false;
	private boolean DesertQuerySignal=false;

	private CTabFolder editTabFolder;

	private Label RealTotalLabel;
	private int realTotalSum = 0;
	private int chosenAppPrice = 0;
	private int chosenDrinkPrice = 0;
	private int chosenMCPrice = 0;
	private int chosenDesPrice = 0;

	private CTabFolder addTabFolder;
	private Button addActorButton;
	private Button addMovieButton;
	private Button addTvShowButton;
	private actorDB newActor;
	private MovieDB newMovie;
	private TVshowDB newTvShow;

	// add connection

	private MovieLink newMovieLink;

	// actor

	private int actorIDForAddConnection;
	private Map<Integer, String> actorsFoundForAddConnection;
	private String[] actorsForAddConnectionNames;
	private actorDB actorToFind_forAddConnection;
	private ActorParameters findActorForAddConnection;
	private Composite selectActorForAddConnectionComposite;
	private Combo actorsFoundForAddConnectionCombo;
	private Button findActorForAddConnectionButton;

	// movie

	private int movieIDforAddConnection;
	private Map<Integer, MovieDB> moviesFoundForAddConnection;
	private String[] moviesForAddConnectionNames;
	private HashMap<String, MovieDB> moviesForAddConnection;
	private MovieDB movieToFind_forAddConnection;
	private MovieParametersComposite findMovieForAddConnection;
	private Composite selectMovieForAddConnectionComposite;
	private Combo moviesFoundForAddConnectionCombo;
	private Button findMovieForAddConnectionButton;

	// music
	private MP3 mp3;

	private String filesPath;

	// 0=find actors for edit, 1=find movies for edit, 2=find tv for edit,
	// 3=find actors for add connection 4=find movies for add connection
	static boolean[] isSearchingArray = new boolean[] { false, false, false,
		false, false };

	@Override
	public void open() {

		shell = SWTUtil.getShell();
		shell.setText("MovEat");
		shell.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD));

		shell.setBackgroundImage(mainBackground);
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		shell.setImage(new Image(SWTUtil.getDisplay(),"resources/icon.ico"));

		GridLayout gridLayout = new GridLayout(1, false);
		shell.setLayout(gridLayout);
		createContent(shell);

		// confirm exit
		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent e) {
				MessageBox mb = new MessageBox(shell, SWT.ICON_QUESTION
						| SWT.OK | SWT.CANCEL);
				mb.setText("Confirm Exit");
				mb.setMessage("Are you sure you want to exit?");
				int rc = mb.open();

				if (rc == SWT.OK) {

					for (boolean isSearching : isSearchingArray) {
						if (isSearching) {
							userMessage(shell, "Still working...",
									"Please wait until search is finished");
							e.doit = false;
							return;
						}
					}

					ker.Stop();
					return;

				}
				e.doit = false;

			}
		});

		mp3 = new MP3("resources/Crazy Frog - I Like To Move It.mp3");
		SWTUtil.openShell(shell);

	}

	private void createContent(Shell shell) {

		tabFolder = new CTabFolder(shell, SWT.BORDER);
		tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
				1, SWT.FILL));
		// tabFolder.setSize(1000,SWT.FILL);
		tabFolder.setSimple(false);// the fansy tab look

		CTabItem orderTabItem = new CTabItem(tabFolder, SWT.NONE);
		orderTabItem.setText("Make an ORDER");
		orderTabItem.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		orderTabItem.setControl(getOrderContol(tabFolder));

		CTabItem importTabItem = new CTabItem(tabFolder, SWT.NULL);
		importTabItem.setText("Import from IMDB");
		importTabItem.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		importTabItem.setControl(getImportControl(tabFolder, shell));

		CTabItem EditTabItem = new CTabItem(tabFolder, SWT.NULL);
		EditTabItem.setText("Edit DATA");
		EditTabItem.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		EditTabItem.setControl(getEditControl(tabFolder));

		Composite logMessegaAndMute = new Composite(shell, SWT.NONE);
		logMessegaAndMute.setLayout(new GridLayout(2, false));
		logMessegaAndMute.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true,
				false, SWT.FILL, SWT.NONE));

		Label logTitle = new Label(logMessegaAndMute, SWT.NONE);
		logTitle.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false,
				SWT.NONE, 1));
		logTitle.setText("Log Messages:");
		logTitle.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		// final Button stopCheckBox = new Button(logMessegaAndMute, SWT.CHECK);
		// stopCheckBox.setLayoutData(new GridData(SWT.RIGHT, SWT.NONE, false,
		// false, SWT.NONE, SWT.NONE));
		// stopCheckBox.setText("Stop Music");
		// stopCheckBox.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
		// SWT.BOLD));
		// stopCheckBox.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent e){
		// if(stopCheckBox.getSelection()){
		// mp3.close();
		// stopCheckBox.setEnabled(false);
		// stopCheckBox.setText("no music");
		// }
		//
		// }
		// });

		logMessage = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.READ_ONLY
				| SWT.V_SCROLL);
		logMessage.setText("\n\n\n\n");
		logMessage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true,
				SWT.FILL, SWT.FILL));

		shell.pack();
	}

	private Control getEditControl(CTabFolder tabFolder) {

		Composite editComposite = new Composite(tabFolder, SWT.NONE);
		editComposite.setLayout(new GridLayout(2, false));

		editTabFolder = new CTabFolder(editComposite, SWT.BORDER);
		editTabFolder
		.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		editTabFolder.setSimple(false);// the fansy tab look

		CTabItem editDeleteExplanationTabItem = new CTabItem(editTabFolder,
				SWT.NONE);
		editDeleteExplanationTabItem.setText("Instructions");
		editDeleteExplanationTabItem.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));
		editDeleteExplanationTabItem
		.setControl(getEditDeleteExplanationControl(editTabFolder));

		CTabItem addTabItem = new CTabItem(editTabFolder, SWT.NONE);
		addTabItem.setText("Add your own DATA");
		addTabItem.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		addTabItem.setControl(getAddContol(editTabFolder));

		CTabItem editDeleteTabItem = new CTabItem(editTabFolder, SWT.NONE);
		editDeleteTabItem.setText("Edit/Delete DATA");
		editDeleteTabItem.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));
		editDeleteTabItem.setControl(getEditDeleteControl(editTabFolder));

		return editComposite;
	}

	private Control getEditDeleteExplanationControl(CTabFolder editTabFolder2) {
		Composite descriptionComposite = new Composite(editTabFolder2, SWT.NONE);
		descriptionComposite.setLayout(new GridLayout());

		Description = new Label(descriptionComposite, SWT.BORDER);
		Description.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, true));

		Description.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10,
				SWT.NONE));

		Description.setText("In this tab you can edit your actors, movies and TV Shows.\n" +
				"You can even decide who will act in your movies!\n\n" +
				"So, you have three options:\n" +
				"You can add a new entity to the system, edit an existing entity or delete one.\n\n" +
				"Add:\n" +
				"		In order to add a new entity, simply choose the 'Add you own Data' Tab, select the entity you would like to add,\n" +
				"		insert the needed data for the chosen entity and click the 'Add!' Buttton.\n" +
				"		To connect an actor to a movie, simply find the actor, find the movie and press 'Act in It!'.\n\n" +
				"Edit\\Delete:\n" +
				"		In order to Edit\\Delete an entity, simply choose the 'Edit\\Delete Data' Tab and select the entity you would like to Edit\\Delete.\n" +
				"		Now, you will first need to find the wanted entity, so insert whatever you know about it and click 'Find XXX'.\n" +
				"		All that's left to do now, is to Choose one from the list, click 'Select' and you will get the Edit\\Delete window.\n" +
				"		In it you can edit the parameters, or just delete it from the DataBase.\n\n" +
				"Have Fun!" );

		return descriptionComposite;

	}

	private Control getEditDeleteControl(CTabFolder editTabFolder2) {

		Composite editDeleteComposite = new Composite(editTabFolder2, SWT.NONE);
		editDeleteComposite.setLayout(new GridLayout(2, false));

		CTabFolder editDeleteTabFolder = new CTabFolder(editDeleteComposite,
				SWT.BORDER);
		editDeleteTabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, true));
		editDeleteTabFolder.setSimple(false);// the fansy tab look

		CTabItem editDeleteActorsTabItem = new CTabItem(editDeleteTabFolder,
				SWT.NONE);
		editDeleteActorsTabItem.setText("Edit/Delete Actors");
		editDeleteActorsTabItem.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));
		editDeleteActorsTabItem
		.setControl(getEditDeleteActorsContol(editDeleteTabFolder));

		CTabItem editDeleteMoviesTabItem = new CTabItem(editDeleteTabFolder,
				SWT.NONE);
		editDeleteMoviesTabItem.setText("Edit/Delete Movies");
		editDeleteMoviesTabItem.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));
		editDeleteMoviesTabItem
		.setControl(getEditDeleteMoviesContol(editDeleteTabFolder));

		CTabItem editDeleteTvShowsTabItem = new CTabItem(editDeleteTabFolder,
				SWT.NONE);
		editDeleteTvShowsTabItem.setText("Edit/Delete TvShows");
		editDeleteTvShowsTabItem.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));
		editDeleteTvShowsTabItem
		.setControl(getEditDeleteTvShowsContol(editDeleteTabFolder));

		return editDeleteComposite;
	}

	private Control getEditDeleteTvShowsContol(CTabFolder father) {
		Composite EditDeleteTvShowComposite = new Composite(father, SWT.NONE);
		EditDeleteTvShowComposite.setLayout(new GridLayout(1, false));

		Composite editComposite = new Composite(EditDeleteTvShowComposite,
				SWT.NONE);
		editComposite.setLayout(new GridLayout(1, false));
		editComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true,
				false));

		initEditTvShowComposite(editComposite);

		return EditDeleteTvShowComposite;

	}

	private void initEditTvShowComposite(Composite father) {

		Composite editTvShowComposite = new Composite(father, SWT.NONE);
		editTvShowComposite.setLayout(new GridLayout(1, false));
		editTvShowComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, false));

		/* edit tv Show step one */

		Label findTvShowLabel = new Label(editTvShowComposite, SWT.NONE);
		findTvShowLabel.setText("1. Please fill the parameters for search.");
		findTvShowLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));

		editTvShow_stepOne = new TvShowParametersComposite(editTvShowComposite,
				true);

		findTvShowButton = new Button(editTvShowComposite, SWT.PUSH);

		findTvShowButton.setText("Find Tv Shows");
		findTvShowButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		findTvShowButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				getUserInputForFindTvShowDB();
				if (tvShowToFind_forEdit != null) {

					isSearchingArray[TV_FOR_EDIT] = true;

					editTvShow_stepOne.setEnabled(false);
					findTvShowButton.setEnabled(false);
					findTvShowButton.setGrayed(true);

					tvShowsFoundCombo.setEnabled(false);
					selectTvShowButton.setEnabled(false);
					selectTvShowButton.setGrayed(true);

					ker.ButtonPushed(Operation.FIND_TV_SHOW);

				}

			}
		});

		Label separator12 = new Label(editTvShowComposite, SWT.HORIZONTAL
				| SWT.SEPARATOR);
		separator12.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false,
				SWT.FILL, SWT.NONE));

		/* Edit movies step two */

		editTvShow_stepTwo = new Composite(editTvShowComposite, SWT.NONE);
		editTvShow_stepTwo.setLayout(new GridLayout(1, false));
		editTvShow_stepTwo.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false,
				false, SWT.NONE, 1));
		editTvShow_stepTwo.setEnabled(false);

		Label selectTheMovieLabel = new Label(editTvShow_stepTwo, SWT.NONE);
		selectTheMovieLabel
		.setText("2. Select the TV Show you wish to edit/delete from the list below");
		selectTheMovieLabel.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));

		tvShowsFoundCombo = new Combo(editTvShow_stepTwo, SWT.NONE);
		tvShowsFoundCombo
		.setText("---------------------------------------------------------");
		tvShowsFoundCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		tvShowsFoundCombo.setEnabled(false);
		tvShowsFoundCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});

		selectTvShowButton = new Button(editTvShow_stepTwo, SWT.PUSH);
		selectTvShowButton.setText("Select TV Show");
		selectTvShowButton.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.NONE));
		selectTvShowButton.setEnabled(false);
		selectTvShowButton.setGrayed(true);

		selectTvShowButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				verifySelectedTvShow();
				if (selectedTvShow != null) {

					editTvShow_stepOne.setEnabled(false);
					findTvShowButton.setEnabled(false);
					findTvShowButton.setGrayed(true);

					editTvShow_stepTwo.setEnabled(false);
					selectTvShowButton.setEnabled(false);
					selectTvShowButton.setGrayed(true);
					tvShowsFoundCombo.setEnabled(false);

					initEditDeleteTvShowWindow();

				}
			}
		});

	}

	private void initEditDeleteTvShowWindow() {

		subShell = SWTUtil.getShell();
		subShell.setBackgroundImage(mainBackground);
		subShell.setBackgroundMode(SWT.INHERIT_DEFAULT);

		subShell.setLayout(new GridLayout());
		subShell.setText("Edit/Delete the Tv Show");

		/* edit tv show step three */

		editTvShow_stepThree = new TvShowParametersComposite(subShell, false);

		editTvShowButton = new Button(subShell, SWT.PUSH);
		editTvShowButton.setText("Edit TV Show!");
		editTvShowButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		editTvShowButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				getUserInputForEditTvShow();

				if (editedTvShow != null) {

					subShell.close();

					ker.ButtonPushed(Operation.EDIT_TV_SHOW);

				}
			}

		});

		deleteTvShowButton = new Button(subShell, SWT.PUSH);
		deleteTvShowButton.setText("Delete the TV Show!");
		deleteTvShowButton.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.NONE));
		deleteTvShowButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				tvShowsFoundCombo
				.setText("---------------------------------------------------------");

				subShell.close();

				ker.ButtonPushed(Operation.DELETE_TV_SHOW);
			}
		});
		subShell.pack();

		fillEditDeleteShellWithData(editTvShow_stepThree);

		subShell.addListener(SWT.Close, new Listener() {

			@Override
			public void handleEvent(Event event) {
				editTvShow_stepOne.setEnabled(true);
				findTvShowButton.setEnabled(true);
				findTvShowButton.setGrayed(false);

				event.doit=true;

				shell.setEnabled(true);
				shell.forceActive();

				subShell.dispose();

			}
		});
		shell.setEnabled(false);
		subShell.open();

	}

	private void fillEditDeleteShellWithData(
			TvShowParametersComposite tvShowParameters) {

		if (selectedTvShow != null) {
			if (selectedTvShow.getImdbName() != null)
				tvShowParameters.setName(selectedTvShow.getImdbName());
			else
				tvShowParameters.setName("NO NAME FOR THIS TV SHOW");

			tvShowParameters.setStars("" + selectedTvShow.getStars());

			if (selectedTvShow.getDecade() != null)
				tvShowParameters.setDecade(selectedTvShow.getDecade()
						.toString());
			else
				tvShowParameters.setDecade("NO DECADE FOR THIS TV SHOW");
		}

	}

	private void getUserInputForEditTvShow() {

		editedTvShow = editTvShow_stepThree.getTvShowFromUserInput_edit();

	}

	private void verifySelectedTvShow() {

		selectedTvShow = null;
		if (tvShowsFoundCombo.getText().equals("Select A TV Show")
				|| tvShowsFoundCombo.getText().equals("")) {
			userMessage(shell, "Not A valid Input", "Please select a TV Show");
			return;
		}

		selectedTvShow = tvShowsForEdit.get(tvShowsFoundCombo.getText());
		selectedTvShow_id = getKeyByValue(tvShowsFoundForEdit, selectedTvShow);

	}

	private void getUserInputForFindTvShowDB() {

		tvShowToFind_forEdit = editTvShow_stepOne.getTvShowFromUserInput_find();

	}

	private Control getEditDeleteActorsContol(CTabFolder editTabFolder) {

		Composite EditDeleteComposite = new Composite(editTabFolder, SWT.NONE);
		EditDeleteComposite.setLayout(new GridLayout(1, false));

		Composite editComposite = new Composite(EditDeleteComposite, SWT.NONE);
		editComposite.setLayout(new GridLayout(1, false));
		editComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true,
				false));

		initEditActorsComposite(editComposite);

		return EditDeleteComposite;

	}

	private void initEditActorsComposite(Composite editComposite) {

		Composite editActors = new Composite(editComposite, SWT.NONE);
		editActors.setLayout(new GridLayout(1, false));
		editActors.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

		Label findActorLabel = new Label(editActors, SWT.NONE);
		findActorLabel
		.setText("1. Please type a name of an actor, or a part of it.");
		findActorLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		editActor_stepOne = new ActorParameters(editActors);

		findActorButton = new Button(editActors, SWT.PUSH);
		findActorButton.setText("Find Actors");
		findActorButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));

		findActorButton.setEnabled(false);
		findActorButton.setGrayed(true);
		findActorButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				verifyUserInputForFindActor();
				if (actorToFind_forEdit != null) {

					editActor_stepOne.setEnabled(false);
					findActorButton.setEnabled(false);
					findActorButton.setGrayed(true);

					actorsFoundCombo.setEnabled(false);
					selectActorButton.setEnabled(false);
					selectActorButton.setGrayed(true);

					isSearchingArray[ACTORS_FOR_EDIT] = true;
					ker.ButtonPushed(Operation.FIND_ACTOR);

				}

			}
		});

		editActor_stepOne.addEnableDisableToRelatedButton(findActorButton);
		Label separator12 = new Label(editActors, SWT.HORIZONTAL
				| SWT.SEPARATOR);
		separator12.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false,
				SWT.FILL, SWT.NONE));

		/* Edit movies step two */

		Label selectTheMovieLabel = new Label(editActors, SWT.NONE);
		selectTheMovieLabel
		.setText("2. Select the actor you wish to edit/delete from the list below");
		selectTheMovieLabel.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));

		editActor_stepTwo = new Composite(editActors, SWT.NONE);
		editActor_stepTwo.setLayout(new GridLayout(1, false));
		editActor_stepTwo.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false,
				false, SWT.NONE, 1));
		editActor_stepTwo.setEnabled(false);

		actorsFoundCombo = new Combo(editActor_stepTwo, SWT.NONE);
		actorsFoundCombo
		.setText("---------------------------------------------------------");
		actorsFoundCombo.setEnabled(false);
		actorsFoundCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		actorsFoundCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});

		selectActorButton = new Button(editActor_stepTwo, SWT.PUSH);
		selectActorButton.setText("Select Actor");
		selectActorButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		selectActorButton.setEnabled(false);
		selectActorButton.setGrayed(true);

		selectActorButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				verifySelectedActor();
				if (selectedActor != null) {

					editActor_stepOne.setEnabled(false);
					findActorButton.setEnabled(false);
					findActorButton.setGrayed(true);

					editActor_stepTwo.setEnabled(false);
					selectActorButton.setEnabled(false);
					selectActorButton.setGrayed(true);
					actorsFoundCombo.setEnabled(false);

					initEditDeleteActorWindow();

				}

			}
		});

	}

	private void initEditDeleteActorWindow() {
		subShell = SWTUtil.getShell();
		subShell.setBackgroundImage(mainBackground);
		subShell.setBackgroundMode(SWT.INHERIT_DEFAULT);

		subShell.setLayout(new GridLayout());
		subShell.setText("Edit/Delete the Actor");

		Label findActorLabel = new Label(subShell, SWT.NONE);
		findActorLabel.setText("1. Please type a name for the Actor.");
		findActorLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		editActorName_edit = new Text(subShell, SWT.BORDER);
		editActorName_edit.setText("The Actor's name");
		editActorName_edit.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.NONE));

		editActorButton = new Button(subShell, SWT.PUSH);
		editActorButton.setText("Edit Actor!");
		editActorButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		editActorButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				getUserInputForEditActor();

				if (editedActor != null) {

					subShell.close();

					ker.ButtonPushed(Operation.EDIT_ACTOR);

				}
			}

		});

		deleteActorButton = new Button(subShell, SWT.PUSH);
		deleteActorButton.setText("Delete the Actor!");
		deleteActorButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		deleteActorButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {

				actorsFoundCombo
				.setText("---------------------------------------------------------");

				subShell.close();
				ker.ButtonPushed(Operation.DELETE_ACTOR);

			}
		});
		subShell.pack();

		editActorName_edit.setText(selectedActor);
		// SWTUtil.openShell(editDeleteActorShell);

		subShell.addListener(SWT.Close, new Listener() {

			@Override
			public void handleEvent(Event event) {
				editActor_stepOne.setEnabled(true);
				findActorButton.setEnabled(true);
				findActorButton.setGrayed(false);



				event.doit=true;

				shell.setEnabled(true);
				shell.forceActive();

				subShell.dispose();
			}
		});

		shell.setEnabled(false);
		subShell.open();

	}

	private void getUserInputForEditActor() {

		editedActor = null;

		String error = "";
		if (editActorName_edit.getText().equals(""))
			error = "Please type a name of the actor.\n";

		if (!Utils.isValidAscii(editActorName_edit.getText()))
			error = "Please write the name of the actor in Ascii (english for example).";

		if (!error.equals("")) {
			userMessage(subShell, "Not A vald Input", error);
			return;
		}

		editedActor = editActorName_edit.getText();

	}

	private void verifySelectedActor() {
		selectedActor = null;
		if (actorsFoundCombo.getText().equals("Select An Actor")
				|| actorsFoundCombo.getText().equals("")) {
			userMessage(shell, "Not A valid Input", "Please select An Actor");
			return;
		}

		selectedActor = actorsFoundCombo.getText();
		selectedActor_id = getKeyByValue(actorsFoundForEdit, selectedActor);

	}

	private void verifyUserInputForFindActor() {

		actorToFind_forEdit = editActor_stepOne
				.getActorDBFromUserInput_find(shell);

	}

	private Control getEditDeleteMoviesContol(CTabFolder editTabFolder) {
		Composite EditDeleteComposite = new Composite(editTabFolder, SWT.NONE);
		EditDeleteComposite.setLayout(new GridLayout(1, false));

		Composite editComposite = new Composite(EditDeleteComposite, SWT.NONE);
		editComposite.setLayout(new GridLayout(1, false));
		editComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true,
				false));

		initEditMoviesComposite(editComposite);

		return EditDeleteComposite;

	}

	private void initEditMoviesComposite(Composite editComposite) {
		Composite editMoviesComposite = new Composite(editComposite, SWT.NONE);
		editMoviesComposite.setLayout(new GridLayout(1, false));
		editMoviesComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE,
				true, false));

		/* edit movies step one */

		Label findMovieLabel = new Label(editMoviesComposite, SWT.NONE);
		findMovieLabel.setText("1. Please fill the parameters for search.");
		findMovieLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		editMovie_stepOne = new MovieParametersComposite(editMoviesComposite,
				true, true);

		findMoviesButton = new Button(editMoviesComposite, SWT.PUSH);

		findMoviesButton.setText("Find movies");
		findMoviesButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		findMoviesButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				getUserInputForFindMovieDB();
				if (movieToFind_forEdit != null) {

					isSearchingArray[MOVIES_FOR_EDIT] = true;

					editMovie_stepOne.setEnabled(false);
					findMoviesButton.setEnabled(false);
					findMoviesButton.setGrayed(true);

					moviesFoundCombo.setEnabled(false);
					selectMovieButton.setEnabled(false);
					selectMovieButton.setGrayed(true);

					ker.ButtonPushed(Operation.FIND_MOVIE);

				}

			}
		});

		Label separator12 = new Label(editMoviesComposite, SWT.HORIZONTAL
				| SWT.SEPARATOR);
		separator12.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false,
				SWT.FILL, SWT.NONE));

		/* Edit movies step two */

		editMovie_stepTwo = new Composite(editMoviesComposite, SWT.NONE);
		editMovie_stepTwo.setLayout(new GridLayout(1, false));
		editMovie_stepTwo.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false,
				false, SWT.NONE, 1));
		editMovie_stepTwo.setEnabled(false);

		Label selectTheMovieLabel = new Label(editMovie_stepTwo, SWT.NONE);
		selectTheMovieLabel
		.setText("2. Select the movie you wish to edit/delete from the list below");
		selectTheMovieLabel.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));

		moviesFoundCombo = new Combo(editMovie_stepTwo, SWT.NONE);
		moviesFoundCombo
		.setText("---------------------------------------------------------");
		moviesFoundCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		moviesFoundCombo.setEnabled(false);
		moviesFoundCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});

		selectMovieButton = new Button(editMovie_stepTwo, SWT.PUSH);
		selectMovieButton.setText("Select movie");
		selectMovieButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		selectMovieButton.setEnabled(false);
		selectMovieButton.setGrayed(true);

		selectMovieButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				verifySelectedMovie();
				if (selectedMovie != null) {

					editMovie_stepOne.setEnabled(false);
					findMoviesButton.setEnabled(false);
					findMoviesButton.setGrayed(true);

					editMovie_stepTwo.setEnabled(false);
					selectMovieButton.setEnabled(false);
					selectMovieButton.setGrayed(true);
					moviesFoundCombo.setEnabled(false);
					initEditDeleteMovieWindow();

				}
			}
		});

	}

	private void verifySelectedMovie() {

		selectedMovie = null;
		if (moviesFoundCombo.getText().equals("Select A movie")
				|| moviesFoundCombo.getText().equals("")) {
			userMessage(shell, "Not A valid Input", "Please select a movie");
			return;
		}

		selectedMovie = moviesForEdit.get(moviesFoundCombo.getText());
		selectedMovie_id = getKeyByValue(moviesFoundForEdit, selectedMovie);
	}

	public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
		for (Entry<T, E> entry : map.entrySet()) {
			if (value.equals(entry.getValue())) {
				return entry.getKey();
			}
		}
		return null;
	}

	private void initEditDeleteMovieWindow() {
		subShell = SWTUtil.getShell();
		subShell.setBackgroundImage(mainBackground);
		subShell.setBackgroundMode(SWT.INHERIT_DEFAULT);

		subShell.setLayout(new GridLayout());
		subShell.setText("Edit/Delete the movie");

		/* edit movies step three */

		editMovie_stepThree = new MovieParametersComposite(subShell, false,
				true);

		editMovieButton = new Button(subShell, SWT.PUSH);
		editMovieButton.setText("Edit movies!");
		editMovieButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		editMovieButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				getUserInputForEditMovieDB();

				if (editedMovie != null) {

					subShell.close();

					ker.ButtonPushed(Operation.EDIT_MOVIE);

				}
			}

		});

		deleteMovieButton = new Button(subShell, SWT.PUSH);
		deleteMovieButton.setText("Delete the Movie!");
		deleteMovieButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));
		deleteMovieButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {

				moviesFoundCombo
				.setText("---------------------------------------------------------");

				subShell.close();
				ker.ButtonPushed(Operation.DELETE_MOVIE);

			}
		});

		subShell.pack();

		fillEditDeleteShellWithData(editMovie_stepThree);
		// SWTUtil.openShell(editDeleteShell);

		subShell.addListener(SWT.Close, new Listener() {

			@Override
			public void handleEvent(Event event) {
				editMovie_stepOne.setEnabled(true);
				findMoviesButton.setEnabled(true);
				findMoviesButton.setGrayed(false);

				event.doit=true;

				shell.setEnabled(true);
				shell.forceActive();

				subShell.dispose();

			}
		});

		shell.setEnabled(false);
		subShell.open();

	}

	private void getUserInputForEditMovieDB() {

		editedMovie = editMovie_stepThree.getMovieDBFromUserInput_edit();
	}

	private void fillEditDeleteShellWithData(
			MovieParametersComposite movieParameter) {
		// MovieDB movieSelected =
		// moviesForEdit.get(moviesFoundCombo.getText());
		movieParameter.putMovie(selectedMovie);

	}

	private void getUserInputForFindMovieDB() {

		movieToFind_forEdit = editMovie_stepOne.getMovieDBFromUserInput_find();

	}

	private Control getAddContol(CTabFolder editTabFolder) {

		Composite addComposite = new Composite(editTabFolder, SWT.NONE);
		addComposite.setLayout(new GridLayout(1, true));

		addTabFolder = new CTabFolder(addComposite, SWT.BORDER);
		addTabFolder
		.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		addTabFolder.setSimple(false);// the fansy tab look

		CTabItem addActorTabItem = new CTabItem(addTabFolder, SWT.NONE);
		addActorTabItem.setText("Add new Actor");
		addActorTabItem.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));
		addActorTabItem.setControl(getAddActorContol(addTabFolder));

		CTabItem addMovieTabItem = new CTabItem(addTabFolder, SWT.NONE);
		addMovieTabItem.setText("Add new Movie");
		addMovieTabItem.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));
		addMovieTabItem.setControl(getAddMovieContol(addTabFolder));

		CTabItem addTvShowTabItem = new CTabItem(addTabFolder, SWT.NONE);
		addTvShowTabItem.setText("Add new TV Show");
		addTvShowTabItem.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));
		addTvShowTabItem.setControl(getAddTvShowContol(addTabFolder));

		CTabItem addPlayInMovieRelationTabItem = new CTabItem(addTabFolder,
				SWT.NONE);
		addPlayInMovieRelationTabItem.setText("Add Actor to Movie");
		addPlayInMovieRelationTabItem.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));
		addPlayInMovieRelationTabItem
		.setControl(getaddPlayInMovieRelationContol(addTabFolder));

		return addComposite;
	}

	private Control getaddPlayInMovieRelationContol(CTabFolder addTabFolder) {

		Composite composite = new Composite(addTabFolder, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));

		Label findActorLabel = new Label(composite, SWT.NONE);
		findActorLabel.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false,
				false, SWT.FILL, SWT.NONE));
		findActorLabel.setText("find the actor");
		findActorLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		Composite findActorForAddConnectionComposite = new Composite(composite,
				SWT.NONE);
		findActorForAddConnectionComposite.setLayout(new GridLayout(1, false));

		Label findActorParametersLabel = new Label(
				findActorForAddConnectionComposite, SWT.NONE);
		findActorParametersLabel.setText("1. Type the name of the actor");
		findActorParametersLabel.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));

		Composite actorNameComposite = new Composite(
				findActorForAddConnectionComposite, SWT.NONE);
		actorNameComposite.setLayout(new RowLayout());

		findActorForAddConnection = new ActorParameters(actorNameComposite);

		findActorForAddConnectionButton = new Button(
				findActorForAddConnectionComposite, SWT.PUSH);
		findActorForAddConnectionButton.setText("Find Actor");
		findActorForAddConnectionButton.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));
		findActorForAddConnectionButton
		.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				verifyActorParametersForAddConnection();
				if (actorToFind_forAddConnection != null) {

					isSearchingArray[ACTORS_FOR_ADDCON] = true;

					findActorForAddConnectionButton.setEnabled(false);
					findActorForAddConnectionButton.setGrayed(true);
					actorsFoundForAddConnectionCombo.setEnabled(false);

					ker.ButtonPushed(Operation.FIND_ACTOR_FOR_ADD_CONNECTION);
				}
			}
		});

		findActorForAddConnectionButton.setEnabled(false);
		findActorForAddConnectionButton.setGrayed(true);

		findActorForAddConnection
		.addEnableDisableToRelatedButton(findActorForAddConnectionButton);

		selectActorForAddConnectionComposite = new Composite(composite,
				SWT.NONE);
		selectActorForAddConnectionComposite
		.setLayout(new GridLayout(1, false));
		selectActorForAddConnectionComposite.setLayoutData(new GridData(
				SWT.NONE, SWT.TOP, false, false, SWT.NONE, 1));
		selectActorForAddConnectionComposite.setEnabled(false);

		Label selectTheActorLabel = new Label(
				selectActorForAddConnectionComposite, SWT.NONE);
		selectTheActorLabel
		.setText("2. Select the Actor you wish to Add to a movie, from the list below.");
		selectTheActorLabel.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));

		actorsFoundForAddConnectionCombo = new Combo(
				selectActorForAddConnectionComposite, SWT.NONE);
		actorsFoundForAddConnectionCombo
		.setText("Select An Actor                     ");
		actorsFoundForAddConnectionCombo.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.NONE));
		actorsFoundForAddConnectionCombo.setEnabled(false);
		actorsFoundForAddConnectionCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});

		Label seperator12 = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
		seperator12.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false,
				SWT.FILL, SWT.NONE));

		Label findMovieLabel = new Label(composite, SWT.NONE);
		findMovieLabel.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false,
				false, SWT.FILL, SWT.NONE));
		findMovieLabel.setText("find the movie");
		findMovieLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.NONE));

		Composite findMovieForAddConnectionComposite = new Composite(composite,
				SWT.NONE);
		findMovieForAddConnectionComposite.setLayout(new GridLayout(1, false));

		Label findMovieParametersLabel = new Label(
				findMovieForAddConnectionComposite, SWT.NONE);
		findMovieParametersLabel
		.setText("3. Insert parameters for the wanted movie");
		findMovieParametersLabel.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.NONE));

		findMovieForAddConnection = new MovieParametersComposite(
				findMovieForAddConnectionComposite, true, false);

		findMovieForAddConnectionButton = new Button(
				findMovieForAddConnectionComposite, SWT.PUSH);
		findMovieForAddConnectionButton.setLayoutData(new GridData(SWT.NONE,
				SWT.NONE, false, true));
		findMovieForAddConnectionButton.setText("Find Movie");

		findMovieForAddConnectionButton.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));
		findMovieForAddConnectionButton
		.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				verifyMovieParametersForAddConnection();
				if (movieToFind_forAddConnection != null) {

					isSearchingArray[MOVIES_FOR_ADDCON] = true;

					findMovieForAddConnectionButton.setEnabled(false);
					findMovieForAddConnectionButton.setGrayed(true);

					moviesFoundForAddConnectionCombo.setEnabled(false);
					ker.ButtonPushed(Operation.FIND_MOVIE_FOR_ADD_CONNECTION);
				}
			}
		});

		selectMovieForAddConnectionComposite = new Composite(composite,
				SWT.NONE);
		selectMovieForAddConnectionComposite
		.setLayout(new GridLayout(1, false));
		selectMovieForAddConnectionComposite.setLayoutData(new GridData(
				SWT.NONE, SWT.TOP, false, false, SWT.NONE, 1));
		selectMovieForAddConnectionComposite.setEnabled(false);

		Label selectTheMovieLabel = new Label(
				selectMovieForAddConnectionComposite, SWT.NONE);
		selectTheMovieLabel
		.setText("4. Select the Movie you wish to Add to an actor, from the list below.");
		selectTheMovieLabel.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));

		moviesFoundForAddConnectionCombo = new Combo(selectMovieForAddConnectionComposite, SWT.NONE);
		moviesFoundForAddConnectionCombo
		.setText("Select A movie                      ");
		moviesFoundForAddConnectionCombo.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.NONE));
		moviesFoundForAddConnectionCombo.setEnabled(false);
		moviesFoundForAddConnectionCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});

		Button connect = new Button(composite, SWT.PUSH);
		connect.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true,
				SWT.NONE, SWT.NONE));
		connect.setText("Act in it!!");
		connect.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		connect.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				if (verifyActorMovieParametersForAddConnection())
					ker.ButtonPushed(Operation.MAKE_ACTOR_MOVIE_CONNECTION);
			}
		});

		return composite;
	}

	private boolean verifyActorMovieParametersForAddConnection() {
		if (actorsFoundForAddConnectionCombo.getText().equals(
				"Select An Actor                     ")
				|| actorsFoundForAddConnectionCombo.getText().equals("")) {
			userMessage(shell, "Error", "Please select an actor");
			return false;
		}

		if (moviesFoundForAddConnectionCombo.getText().equals(
				"Select A movie                      ")
				|| moviesFoundForAddConnectionCombo.getText().equals("")) {
			userMessage(shell, "Error", "Please select a movie");
			return false;
		}

		MovieDB selectedMovie = moviesForAddConnection
				.get(moviesFoundForAddConnectionCombo.getText());

		movieIDforAddConnection = getKeyByValue(moviesFoundForAddConnection,
				selectedMovie);
		actorIDForAddConnection = getKeyByValue(actorsFoundForAddConnection,
				actorsFoundForAddConnectionCombo.getText());

		return true;

	}

	private void verifyMovieParametersForAddConnection() {
		movieToFind_forAddConnection = findMovieForAddConnection
				.getMovieDBFromUserInput_find();

	}

	private void verifyActorParametersForAddConnection() {
		actorToFind_forAddConnection = findActorForAddConnection
				.getActorDBFromUserInput_find(shell);
	}

	private Control getAddTvShowContol(CTabFolder father) {

		Composite addTvShowComposite = new Composite(father, SWT.NONE);
		addTvShowComposite.setLayout(new GridLayout(1, false));
		addTvShowComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true,
				false));

		Label newTvShowLabel = new Label(addTvShowComposite, SWT.NONE);
		newTvShowLabel.setText("Please fill the parameters for new TV Show:");
		newTvShowLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		final TvShowParametersComposite addTvShowParamComposite = new TvShowParametersComposite(
				addTvShowComposite, false);

		addTvShowButton = new Button(addTvShowComposite, SWT.PUSH);
		addTvShowButton.setText("Add TV Show");
		addTvShowButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.NONE));

		// addTvShowButton.setGrayed(true);
		// addTvShowButton.setEnabled(false);

		addTvShowButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				newTvShow = addTvShowParamComposite
						.getTvShowFromUserInput_add();
				if (newTvShow != null)
					ker.ButtonPushed(Operation.ADD_TV_SHOW);
			}
		});

		return addTvShowComposite;
	}

	private Control getAddMovieContol(CTabFolder addTabFolder) {
		Composite com = new Composite(addTabFolder, SWT.NONE);
		com.setLayout(new GridLayout());

		Label addMovieLabel = new Label(com, SWT.NONE);
		addMovieLabel.setText("Please fill the parameters for new Movie");
		addMovieLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		final MovieParametersComposite addMovieComposite = new MovieParametersComposite(
				com, true, true);

		addMovieButton = new Button(com, SWT.PUSH);
		addMovieButton.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false,
				false));
		addMovieButton.setText("Add Movie");
		addMovieButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		addMovieButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {

				newMovie = addMovieComposite.getMovieDBFromUserInput_add();
				if (newMovie != null)
					ker.ButtonPushed(Operation.ADD_MOVIE);

			}
		});

		return com;
	}

	private Control getAddActorContol(CTabFolder addTabFolder) {
		Composite AddActorComposite = new Composite(addTabFolder, SWT.NONE);
		AddActorComposite.setLayout(new GridLayout(2, false));

		Label title = new Label(AddActorComposite, SWT.NONE);
		title.setText("New Actor name:");
		title.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false,
				SWT.FILL, SWT.NONE));
		title.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD));

		final ActorParameters newActorParam = new ActorParameters(
				AddActorComposite);

		addActorButton = new Button(AddActorComposite, SWT.PUSH);
		addActorButton.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false,
				false));
		addActorButton.setText("Add Actor");
		addActorButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		addActorButton.setEnabled(false);
		addActorButton.setGrayed(true);

		newActorParam.addEnableDisableToRelatedButton(addActorButton);

		addActorButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				newActor = new actorDB(newActorParam.getFirstName(),
						newActorParam.getLastName());

				if (!Utils.isValidAscii(newActor.getFirstName())
						|| !Utils.isValidAscii(newActor.getLastName())) {
					userMessage(shell, "Not Ascii",
							"Please write in Ascii (english for example).");
					return;
				}

				ker.ButtonPushed(Operation.ADD_ACTOR);
				addActorButton.setEnabled(false);
				addActorButton.setGrayed(true);
			}
		});
		return AddActorComposite;
	}

	private Control getImportControl(CTabFolder tabFolder, final Shell shell) {
		Composite composite = new Composite(tabFolder, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));

		Button UpdateButton = new Button(composite, SWT.TOGGLE);
		UpdateButton.setText("Import / Update");
		UpdateButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,SWT.BOLD));
		UpdateButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,	true));

		Link aboutUpdate = new Link(composite, SWT.BORDER);
		aboutUpdate.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10,SWT.NONE));

		aboutUpdate.setText("\nAbout update:\n\n" +
				"The import process keeps your menu always up to date.\n" +
				"It relies on IMDB format files that can be found in:\n\t" +
				"<a>http://www.imdb.com/interfaces</a>\n" +
				"In the beginning of the process you'll choose the local folder in which the files are located.\n" +
				"Verify that the folder contains the following files:\nmovies, actors, actresses, ratings, countries, genres, mpaa-ratings-reasons, keywords.\n" +
				"The files can be either compressed: .gz or .zip or text files.\n" +
				"Make sure to configure in the configuration file the desired extension, the default is .list.gz\n\n" +
				"Please note that the process lasts approximately 15 minutes.\n");

		aboutUpdate.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		UpdateButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {

				DirectoryDialog dialog = new DirectoryDialog(shell);

				dialog.setMessage("Please navigate to the folder where you keep the dataBase files.\n");

				filesPath = dialog.open();

				if (filesPath == null) {

					MessageBox mb = new MessageBox(shell, SWT.ICON_QUESTION
							| SWT.OK | SWT.CANCEL);
					mb.setText("Confirmation");
					mb.setMessage("you did not select a folder, the process will use the path that is in the configuration file.\nPress OK to confirm, or CANCEL to go back");
					int rc = mb.open();
					if (rc == SWT.CANCEL) {
						return;
					}
				}
				MessageBox mb = new MessageBox(shell, SWT.ICON_QUESTION
						| SWT.OK | SWT.CANCEL);
				mb.setText("Confirm Update");
				mb.setMessage("Are you sure you want to update?\n(this may take a while..)");
				int rc = mb.open();
				if (rc == SWT.OK) {
					ker.ButtonPushed(Operation.IMPORT);
				}
			}
		});

		return composite;
	}

	private Control getOrderContol(CTabFolder tabFolder) {

		Composite composite = new Composite(tabFolder, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(SWT.TOP);

		initOrderDishesControl(composite);
		initDescriptionControl(composite);

		return composite;
	}

	private void initDescriptionControl(Composite father) {

		Composite com = new Composite(father, SWT.NONE);
		com.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		com.setLayout(new GridLayout(1, false));

		Image logo = new Image(SWTUtil.getDisplay(),
				"resources/logo2.jpg");

		Button logoButton = new Button(com, SWT.PUSH);
		logoButton
		.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false));
		logoButton.setImage(logo);

		logoButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				initAboutShell();

			}
		});

		Description = new Label(com, SWT.BORDER);
		Description.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, true));

		Description.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10,SWT.NONE));

		Description.setText("\tWELCOME to MovEat!!!\n"
				+ "The place where you can eat your favorite movies.\n"
				+ "As the hebrew slang-phrase says, there are cases in your life when you might\n"
				+ " 'eat-a-movie' on them.\n"
				+ "We believe that if so, why couldn't we choose which movie we will eat!\n\n"
				+ "How does it work:\n"
				+ "\t- As an Appetizer:\n"
				+ "We offer you variety of TV Shows!\n"
				+ "Choose your favorite period of time with the approximate price.\n"
				+ "\t- For the Main Course:\n"
				+ "You can choose your main course according to you favorite actor,\n"
				+ "or by preferred food origin and its flavor. In this case add your approximate price.\n"
				+ "\t- and Drink:\n"
				+ "Choose your desired drink type, with the approximate price,\n"
				+ "and we will supply the 'liquid-movies' that feet your preferencese.\n"
				+ "ENJOY!!!");

		Composite totalBillcomp = new Composite(com, SWT.BORDER);
		totalBillcomp.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, true));
		totalBillcomp.setLayout(new GridLayout(2, true));

		totalBillLabel = new Label(totalBillcomp, SWT.NONE);
		totalBillLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		totalBillLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10,SWT.BOLD));
		totalBillLabel.setText("TOTAL BILL:\t$"+totalBillSum+"        ");

		payButton = new Button(totalBillcomp, SWT.PUSH);
		payButton.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, false));
		payButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10,SWT.BOLD));
		payButton.setText("PAY");
		payButton.setEnabled(false);
		payButton.setGrayed(true);

		payButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				subShell = PayShell.getShell(new MP3("resources/MoneyForNothing.mp3"));

				subShell.addListener(SWT.Close, new Listener() {

					@Override
					public void handleEvent(Event event) {

						event.doit=true;

						shell.setEnabled(true);
						shell.forceActive();

						subShell.dispose();
					}
				});

				shell.setEnabled(false);
				subShell.open();

				totalOrderSum=0;
				totalBillSum=0;
				totalBillLabel.setText("TOTAL BILL:\t$0");	

				payButton.setEnabled(false);
				payButton.setGrayed(true);

			}

		});


	}

	protected void initAboutShell() {
		subShell = SWTUtil.getShell(SWT.CLOSE);
		subShell.setBackgroundImage(mainBackground);
		subShell.setBackgroundMode(SWT.INHERIT_DEFAULT);

		subShell.setText("About movEat");
		subShell.setLayout(new GridLayout());

		Label ver = new Label(subShell, SWT.CENTER);
		ver.setLayoutData(new GridData(SWT.CENTER));
		ver.setText("version: 1.0.0");

		Label who = new Label(subShell, SWT.NONE);
		who.setLayoutData(new GridData());
		who.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 10,SWT.NONE));
		who.setText("\nCreated by:\n\tAlona Avruch,\n\tAsaf Ziv,\n\tOmer Ben Rei,\n\tShachaf Ben-Yosef;");

		subShell.addListener(SWT.Close, new Listener() {

			@Override
			public void handleEvent(Event event) {

				event.doit=true;

				shell.setEnabled(true);
				shell.forceActive();

				subShell.dispose();
			}
		});
		shell.setEnabled(false);

		subShell.pack();
		subShell.open();
	}

	/* results control */
	private void initResultsControl() {

		subShell = SWTUtil.getShell(SWT.TITLE);

		subShell.setText("Your Order");
		subShell.setLayout(new GridLayout(1, false));
		subShell.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,SWT.BOLD));

		Image background = new Image(SWTUtil.getDisplay(),"resources/bg2.jpg");
		subShell.setBackgroundImage(background);
		subShell.setBackgroundMode(SWT.INHERIT_DEFAULT);

		Label title = new Label(subShell, SWT.NONE);
		title.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
		title.setText("Here are the results:");
		title.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD));

		Composite resultComposite = new Composite(subShell, SWT.BORDER);
		resultComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));

		resultComposite.setLayout(new GridLayout(1, false));

		if (orderDetails.GetAppetizerDetails() != null) {
			createAppetizerResultsControl(resultComposite);
		}
		if (orderDetails.GetMainCourseDetails() != null) {
			createMainCourseResultsControl(resultComposite);
		}

		if (orderDetails.GetDrinksDetails() != null) {
			createDrinksResultsControl(resultComposite);
		}

		if (true) {
			orderDetails.setDessert(true);

			createDessertResultsControl(resultComposite);
		}

		createOptionsResultsControl(subShell);

		subShell.pack();

		subShell.addListener(SWT.Close, new Listener() {

			@Override
			public void handleEvent(Event event) {

				realTotalSum = chosenAppPrice = chosenDesPrice = chosenDrinkPrice = chosenMCPrice = 0;

				event.doit=true;

				shell.setEnabled(true);
				shell.forceActive();

				subShell.dispose();
			}
		});

		shell.setEnabled(false);
		subShell.open();

	}

	private void createOptionsResultsControl(Composite resultComposite) {

		Composite optionsComposite = new Composite(resultComposite, SWT.NONE);
		optionsComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false));
		optionsComposite.setLayout(new GridLayout(3, true));

		backButton = new Button(optionsComposite, SWT.PUSH);
		backButton.setText("back");
		backButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD));
		backButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true));

		backButton.setEnabled(false);
		backButton.setGrayed(true);

		backButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				subShell.close();

				realTotalSum = chosenAppPrice = chosenDesPrice = chosenDrinkPrice = chosenMCPrice = 0;
				appQuerySignal=MCQuerySignal=drinkQuerySignal=DesertQuerySignal=false;

				desCombo=null;


			}
		});

		RealTotalLabel = new Label(optionsComposite, SWT.NONE);
		RealTotalLabel.setText("Total: $0");
		RealTotalLabel.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,	SWT.BOLD));
		RealTotalLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,true));

		eatButton = new Button(optionsComposite, SWT.PUSH);
		eatButton.setText("Eat your Movies!!!");
		eatButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,SWT.BOLD));
		eatButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
		eatButton.setEnabled(false);
		eatButton.setGrayed(true);
		eatButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				totalBillSum +=realTotalSum;
				totalBillLabel.setText("TOTAL BILL:\t$"+totalBillSum);
				realTotalSum = chosenAppPrice = chosenDesPrice = chosenDrinkPrice = chosenMCPrice = 0;
				appQuerySignal=MCQuerySignal=drinkQuerySignal=DesertQuerySignal=false;

				subShell.close();
				subShell = EatShell.getShell(mp3);

				subShell.addListener(SWT.Close, new Listener() {

					@Override
					public void handleEvent(Event event) {
						event.doit=true;

						shell.setEnabled(true);
						shell.forceActive();

						subShell.dispose();
					}
				});


				shell.setEnabled(false);
				subShell.open();

				payButton.setEnabled(true);
				payButton.setGrayed(false);

				desCombo=null;
			}

		});

	}

	private void createAppetizerResultsControl(Composite resultsComposite) {

		Composite CourseResultComposite = new Composite(resultsComposite,
				SWT.NONE);
		CourseResultComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE,false, false));
		CourseResultComposite.setLayout(new GridLayout(2, true));

		Label title = new Label(CourseResultComposite, SWT.NONE);
		title.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		title.setText("Appetizers:");
		title.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD));

		appCombo = new Combo(CourseResultComposite, SWT.DROP_DOWN);
		appCombo.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

		appCombo.setText("----------------------------------------------------------------------");
		appCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		appCombo.setEnabled(false);

		appCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				int price = appCombo.getSelectionIndex();
				if(price!=-1)
				{
					if (appQuerySignal&&MCQuerySignal&&DesertQuerySignal&&drinkQuerySignal) 
					{
						eatButton.setEnabled(true);
						eatButton.setGrayed(false);
					}

					chosenAppPrice = appResultList.get(price).getSecond();

					realTotalSum = chosenAppPrice + chosenDrinkPrice
							+ chosenMCPrice + chosenDesPrice;

					RealTotalLabel.setText("Total:$" + realTotalSum);

					if (desCombo!=null){
						if(realTotalSum>99)
							desCombo.setEnabled(true);
						else
							desCombo.setEnabled(false);
					}
				}
			}
		});

		appCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});
	}

	private void createMainCourseResultsControl(Composite resultsComposite) {

		Composite CourseResultComposite = new Composite(resultsComposite,
				SWT.NONE);
		CourseResultComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE,
				false, false));
		CourseResultComposite.setLayout(new GridLayout(2, true));

		Label title = new Label(CourseResultComposite, SWT.NONE);
		title.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		title.setText("Main Courses:");
		title.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD));

		McCombo = new Combo(CourseResultComposite, SWT.DROP_DOWN);
		McCombo.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

		McCombo.setText("----------------------------------------------------------------------");
		McCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		McCombo.setEnabled(false);

		McCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				int price = McCombo.getSelectionIndex();
				if(price!=-1)
				{

					if (appQuerySignal&&MCQuerySignal&&DesertQuerySignal&&drinkQuerySignal)
					{
						eatButton.setEnabled(true);
						eatButton.setGrayed(false);
					}

					chosenMCPrice = MCResultList.get(price).getSecond();

					realTotalSum = chosenAppPrice + chosenDrinkPrice
							+ chosenMCPrice + chosenDesPrice;

					RealTotalLabel.setText("Total:$" + realTotalSum);

					if(desCombo!=null){
						if(realTotalSum>99)
							desCombo.setEnabled(true);
						else
							desCombo.setEnabled(false);
					}
				}
			}
		});

		McCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});

	}

	private void createDessertResultsControl(Composite resultsComposite) {

		Composite CourseResultComposite = new Composite(resultsComposite,SWT.NONE);
		CourseResultComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE,false, false));
		CourseResultComposite.setLayout(new GridLayout(2, true));

		Label title = new Label(CourseResultComposite, SWT.NONE);
		title.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		title.setText("Deserts: [OTH!!!]");
		title.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD));

		desCombo = new Combo(CourseResultComposite, SWT.DROP_DOWN);
		desCombo.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

		desCombo.setText("----------------------------------------------------------------------");
		desCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,SWT.BOLD));

		desCombo.setEnabled(false);

		desCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				int price = desCombo.getSelectionIndex();
				if(price!=-1)
				{
					chosenDesPrice = desertResultList.get(price).getSecond();

					realTotalSum = chosenAppPrice + chosenDrinkPrice
							+ chosenMCPrice + chosenDesPrice;

					RealTotalLabel.setText("Total:$" + realTotalSum);

				}
			}
		});

		desCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});
	}

	private void createDrinksResultsControl(Composite resultsComposite) {
		Composite CourseResultComposite = new Composite(resultsComposite,
				SWT.NONE);
		CourseResultComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE,
				false, false));
		CourseResultComposite.setLayout(new GridLayout(2, true));

		Label title = new Label(CourseResultComposite, SWT.NONE);
		title.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		title.setText("Drinks:");
		title.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD));

		drinkCombo = new Combo(CourseResultComposite, SWT.DROP_DOWN);
		drinkCombo.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

		drinkCombo
		.setText("----------------------------------------------------------------------");
		drinkCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		drinkCombo.setEnabled(false);
		drinkCombo.setEnabled(false);

		drinkCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				int price = drinkCombo.getSelectionIndex();
				if(price!=-1)
				{
					if (appQuerySignal&&MCQuerySignal&&DesertQuerySignal&&drinkQuerySignal)
					{
						eatButton.setEnabled(true);
						eatButton.setGrayed(false);
					}

					chosenDrinkPrice = drinkResultList.get(price).getSecond();

					realTotalSum = chosenAppPrice + chosenDrinkPrice
							+ chosenMCPrice + chosenDesPrice;

					RealTotalLabel.setText("Total:$" + realTotalSum);

					if (desCombo!=null){
						if(realTotalSum>99)
							desCombo.setEnabled(true);
						else 
							desCombo.setEnabled(false);
					}
				}
			}
		});

		drinkCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});
	}

	/* ordering control */
	private void initOrderDishesControl(Composite father) {

		Composite orders = new Composite(father, SWT.NONE);
		orders.setLayout(new GridLayout(1, false));
		orders.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false, false));

		initAppetizerComposite(orders);
		initMainCourseComposite(orders);
		initDrinksComposite(orders);

		Composite orderPanelControl = new Composite(orders, SWT.BORDER);
		orderPanelControl.setLayout(new GridLayout(2, false));
		orderPanelControl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));
		Label sale = new Label(orderPanelControl, SWT.BORDER);
		sale.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				SWT.NONE, SWT.FILL));
		sale.setText("SALE!!!\nDesert for FREE with order of $100 or more!");
		sale.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.BOLD| SWT.ITALIC));

		totalOrder = new Label(orderPanelControl, SWT.NONE);
		totalOrder.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false));
		totalOrder.setText("Total: -----");
		totalOrder.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		orderButton = new Button(orderPanelControl, SWT.PUSH);
		orderButton.setText("order");
		orderButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		orderButton.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true,
				true));
		orderButton.setEnabled(false);
		orderButton.setGrayed(true);
		orderButton.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				getOrderDetailsFromControls();
				if (orderDetails != null) {
					ker.ButtonPushed(Operation.ORDER);

					if(!appButtonCheck.getSelection())
						appQuerySignal=true;
					if(!mainCourseButtonCheck.getSelection())
						MCQuerySignal=true;
					if(!drinksButtonCheck.getSelection())
						drinkQuerySignal=true;					
					if(totalOrderSum<100)
						DesertQuerySignal=true;

					initResultsControl();
				}

			}
		});
	}

	private void initAppetizerComposite(Composite father) {

		Composite appetizerComposite = new Composite(father, SWT.NONE
				| SWT.NONE);

		appetizerComposite.setLayout(new GridLayout(1, false));
		appetizerComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		appetizerComposite.setLayout(new GridLayout(1, true));

		appButtonCheck = new Button(appetizerComposite, SWT.CHECK);
		appButtonCheck.setText("Apitizer");
		appButtonCheck.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		final Composite orderComposite = new Composite(appetizerComposite,
				SWT.BORDER);
		orderComposite.setLayout(new GridLayout(2, true));

		orderComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		orderComposite.setEnabled(false);

		Composite radios = new Composite(orderComposite, SWT.NONE | SWT.NONE);
		FillLayout radiosLayOut = new FillLayout(SWT.VERTICAL);
		radiosLayOut.spacing = 10;
		radios.setLayout(radiosLayOut);

		firstDecadeRadio = new Button(radios, SWT.RADIO);
		firstDecadeRadio.setText("until 80's");
		firstDecadeRadio.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));

		secondDecadeRadio = new Button(radios, SWT.RADIO);
		secondDecadeRadio.setText("90's");
		secondDecadeRadio.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));

		thirdDecadeRadio = new Button(radios, SWT.RADIO);
		thirdDecadeRadio.setText("2000's");
		thirdDecadeRadio.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));

		Composite priceComposite = new Composite(orderComposite, SWT.NONE);
		priceComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));
		FillLayout priceLayOut = new FillLayout(SWT.VERTICAL);
		priceLayOut.spacing = 10;
		priceComposite.setLayout(priceLayOut);

		appPrice = new Scale(priceComposite, SWT.NONE);
		appPrice.setMinimum(0);
		appPrice.setMaximum(100);
		appPrice.setIncrement(1);
		appPrice.setToolTipText("price");
		appPriceValue = new Text(priceComposite, SWT.READ_ONLY | SWT.CENTER);
		appPriceValue.setText("Choose your price rate");
		appPriceValue.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		appPrice.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
				int perspectiveValue = appPrice.getSelection();
				appPriceValue.setText("Your choise: $" + perspectiveValue);
				appOrderSum = perspectiveValue;
				totalOrderSum = appOrderSum + mainCourseOrderSum
						+ drinksOrderSum;
				totalOrder.setText("Total:$" + totalOrderSum);
			}
		});

		appButtonCheck.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {

				if (appButtonCheck.getSelection()) {
					appOrderSum = prevAppOrderSum;
					totalOrderSum += appOrderSum;
					totalOrder.setText("Total:$" + totalOrderSum);

					orderComposite.setEnabled(true);
					orderButton.setEnabled(true);
					orderButton.setGrayed(false);

				} else {
					prevAppOrderSum = appOrderSum;
					totalOrderSum -= appOrderSum;
					appOrderSum = 0;
					totalOrder.setText("Total:$" + totalOrderSum);
					orderComposite.setEnabled(false);
					if ((!mainCourseButtonCheck.getSelection())
							&& (!drinksButtonCheck.getSelection())) {
						orderButton.setEnabled(false);
						orderButton.setGrayed(true);

					}
				}

			}
		});

	}

	private void initMainCourseComposite(Composite father) {

		Composite mainCourseComposite = new Composite(father, SWT.NONE
				| SWT.NONE);
		mainCourseComposite.setLayout(new GridLayout(1, false));
		mainCourseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, true));

		Composite buttonsTitle = new Composite(mainCourseComposite, SWT.NONE);
		buttonsTitle.setLayout(new RowLayout());
		buttonsTitle.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				false, SWT.FILL, 1));

		mainCourseButtonCheck = new Button(buttonsTitle, SWT.CHECK);
		// mainCourseButtonCheck.setLayoutData(new GridData(SWT.LEFT,
		// SWT.CENTER, false, false, SWT.FILL, 1));
		mainCourseButtonCheck.setText("Main Course");
		mainCourseButtonCheck.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));

		byActor = new Button(buttonsTitle, SWT.RADIO);
		byActor.setText("order by Spice(actor)");
		byActor.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		byActor.setEnabled(false);
		byActor.setGrayed(true);

		byActor.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				if (byActor.getSelection()) {
					locateActor.setEnabled(true);
					locateActor.setGrayed(false);

					FusionButton.setEnabled(false);
					FusionButton.setGrayed(true);
					countriesCombo.setEnabled(false);
					genresCombo.setEnabled(false);
					mainCoursePrice.setEnabled(false);

				}
			}
		});

		byActor.setSelection(true);
		byOtherParameters = new Button(buttonsTitle, SWT.RADIO);
		byOtherParameters.setText("order by others");
		byOtherParameters.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));

		byOtherParameters.setEnabled(false);
		byOtherParameters.setGrayed(true);

		byOtherParameters.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				if (byOtherParameters.getSelection()) {
					FusionButton.setEnabled(true);
					FusionButton.setGrayed(false);
					if (!FusionButton.getSelection())
						countriesCombo.setEnabled(true);
					genresCombo.setEnabled(true);
					mainCoursePrice.setEnabled(true);

					locateActor.setEnabled(false);
					locateActor.setGrayed(true);

				}
			}
		});

		mainCourseButtonCheck.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				if (mainCourseButtonCheck.getSelection()) {
					byOtherParameters.setEnabled(true);
					byOtherParameters.setGrayed(false);
					byActor.setEnabled(true);
					byActor.setGrayed(false);
				} else {
					byOtherParameters.setEnabled(false);
					byOtherParameters.setGrayed(true);
					byActor.setEnabled(false);
					byActor.setGrayed(true);
				}
			}
		});

		final Composite orderComposite = new Composite(mainCourseComposite,
				SWT.BORDER);
		orderComposite.setLayout(new GridLayout(2, true));
		orderComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));
		orderComposite.setEnabled(false);

		Composite combos = new Composite(orderComposite, SWT.NONE);

		combos.setLayout(new GridLayout(2, false));

		// actorsListButton = new Button(combos, SWT.CHECK);
		// actorsListButton.setText("My Favorites");
		// actorsListButton.setFont(new Font(SWTUtil.getDisplay(),
		// "Segoe Print", 8, SWT.BOLD));
		// actorsListButton.addSelectionListener(new SelectionAdapter() {
		// @Override
		// public void widgetSelected(SelectionEvent e){
		// if (actorsListButton.getSelection())
		// actorsCombo.setItems(OrderingResources.getActors());
		// else actorsCombo.setItems(OrderingResources.getDbBestActors());
		// actorsCombo.setText("Select your favorite spice");
		// }
		// });
		//
		// actorsCombo = new Combo(combos, SWT.DROP_DOWN);
		// actorsCombo.setLayoutData(new
		// GridData(SWT.FILL,SWT.NONE,false,false));
		// String[] actors = getActors();
		// actorsCombo.setItems(actors);
		// actorsCombo.setText("Select your favorite spice");
		// actorsCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
		// SWT.BOLD));
		// actorsCombo.addKeyListener(new KeyAdapter() {
		// public void keyPressed(KeyEvent e){
		// e.doit=false;
		// }
		// });

		locateActor = new Button(combos, SWT.PUSH);
		locateActor.pack();
		locateActor.setText("Locate actor");
		locateActor.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		selectedActorForMainCourse = new Label(combos, SWT.READ_ONLY
				| SWT.BORDER);
		selectedActorForMainCourse.setLayoutData(new GridData(SWT.FILL,
				SWT.NONE, false, false, SWT.NONE, SWT.NONE));
		selectedActorForMainCourse.setText("Selected Spice");
		selectedActorForMainCourse.setFont(new Font(SWTUtil.getDisplay(),
				"Segoe Print", 8, SWT.BOLD));

		locateActor.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				findActorForMainCourse = new FindActorForMainCourse(ker,
						selectedActorForMainCourse);
			}
		});

		FusionButton = new Button(combos, SWT.CHECK);
		FusionButton.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false,
				false, 1, 2));
		FusionButton.setText("Fusion  /");
		FusionButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		FusionButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (FusionButton.getSelection())
					countriesCombo.setEnabled(false);
				else
					countriesCombo.setEnabled(true);

			}
		});

		countriesCombo = new Combo(combos, SWT.DROP_DOWN);
		countriesCombo.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false,
				false, SWT.NONE, SWT.NONE));
		String[] countries = getCountries();
		countriesCombo.setItems(countries);
		countriesCombo.setText("Select your favorite cuisine");
		countriesCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		countriesCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});

		genresCombo = new Combo(combos, SWT.DROP_DOWN);
		genresCombo.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false,
				false, SWT.NONE, 1));
		String[] genres = getGenres();
		genresCombo.setItems(genres);
		genresCombo.setText("Select your favorite type");
		genresCombo.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		genresCombo.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				e.doit = false;
			}
		});

		Composite priceComposite = new Composite(orderComposite, SWT.None);
		FillLayout priceLayOut = new FillLayout(SWT.VERTICAL);
		priceLayOut.spacing = 10;
		priceComposite.setLayout(priceLayOut);
		priceComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		mainCoursePrice = new Scale(priceComposite, SWT.NONE);
		mainCoursePrice.setMinimum(0);
		mainCoursePrice.setMaximum(100);
		mainCoursePrice.setIncrement(1);
		mainCoursePrice.setToolTipText("price");
		mainPriceValue = new Text(priceComposite, SWT.READ_ONLY | SWT.CENTER);
		mainPriceValue.setText("Choose your price rate");
		mainPriceValue.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));
		mainCoursePrice.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
				int perspectiveValue = mainCoursePrice.getSelection();
				mainPriceValue.setText("Your choise: $" + perspectiveValue);
				mainCourseOrderSum = perspectiveValue;
				totalOrderSum = appOrderSum + mainCourseOrderSum
						+ drinksOrderSum;
				totalOrder.setText("Total:$" + totalOrderSum);
			}
		});

		FusionButton.setEnabled(false);
		FusionButton.setGrayed(true);
		countriesCombo.setEnabled(false);
		genresCombo.setEnabled(false);
		mainCoursePrice.setEnabled(false);

		mainCourseButtonCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				if (mainCourseButtonCheck.getSelection()) {
					mainCourseOrderSum = prevMainOrderSum;
					totalOrderSum += mainCourseOrderSum;
					totalOrder.setText("Total:$" + totalOrderSum);

					orderComposite.setEnabled(true);
					orderButton.setEnabled(true);
					orderButton.setGrayed(false);

				} else {
					prevMainOrderSum = mainCourseOrderSum;
					totalOrderSum -= mainCourseOrderSum;
					mainCourseOrderSum = 0;
					totalOrder.setText("Total:$" + totalOrderSum);
					orderComposite.setEnabled(false);
					if ((!appButtonCheck.getSelection())
							&& (!drinksButtonCheck.getSelection())) {
						orderButton.setEnabled(false);
						orderButton.setGrayed(true);

					}
				}
			}
		});

	}

	private void initDrinksComposite(Composite father) {

		Composite drinksComposite = new Composite(father, SWT.NONE | SWT.NONE);
		drinksComposite.setLayout(new GridLayout(1, false));
		drinksComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		drinksButtonCheck = new Button(drinksComposite, SWT.CHECK);
		drinksButtonCheck.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
				false, false, SWT.FILL, 1));
		drinksButtonCheck.setText("Drinks");
		drinksButtonCheck.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));

		final Composite orderComposite = new Composite(drinksComposite,
				SWT.BORDER);
		GridLayout orderCompositeLayOut = new GridLayout(2, false);
		orderCompositeLayOut.makeColumnsEqualWidth = true;
		orderComposite.setLayout(orderCompositeLayOut);
		orderComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));
		orderComposite.setEnabled(false);

		Composite radios = new Composite(orderComposite, SWT.NONE);
		FillLayout radiosLayOut = new FillLayout(SWT.VERTICAL);
		radiosLayOut.spacing = 10;
		radios.setLayout(radiosLayOut);

		alcoholButton = new Button(radios, SWT.RADIO);
		alcoholButton.setText("Alcoholic");
		alcoholButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		softDrinksButton = new Button(radios, SWT.RADIO);
		softDrinksButton.setText("Soft Drinks");
		softDrinksButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));

		cofeeButton = new Button(radios, SWT.RADIO);
		cofeeButton.setText("Cofee");
		cofeeButton.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8,
				SWT.BOLD));

		Composite priceComposite = new Composite(orderComposite, SWT.NONE);
		FillLayout priceLayOut = new FillLayout(SWT.VERTICAL);
		priceLayOut.spacing = 10;
		priceComposite.setLayout(priceLayOut);
		priceComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));

		drinksPrice = new Scale(priceComposite, SWT.NONE);
		drinksPrice.setMinimum(0);
		drinksPrice.setMaximum(100);
		drinksPrice.setIncrement(1);
		drinksPrice.setToolTipText("price");

		drinksPriceValue = new Text(priceComposite, SWT.READ_ONLY | SWT.CENTER);
		drinksPriceValue.setText("Choose your price rate");
		drinksPriceValue.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
				8, SWT.BOLD));
		drinksPrice.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(org.eclipse.swt.widgets.Event arg0) {
				int perspectiveValue = drinksPrice.getSelection();
				drinksPriceValue.setText("Your choise: $" + perspectiveValue);
				drinksOrderSum = perspectiveValue;
				totalOrderSum = appOrderSum + mainCourseOrderSum
						+ drinksOrderSum;
				totalOrder.setText("Total:$" + totalOrderSum);
			}
		});

		drinksButtonCheck.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				if (drinksButtonCheck.getSelection()) {
					drinksOrderSum = prevDrinkOrderSum;
					totalOrderSum += drinksOrderSum;
					totalOrder.setText("Total:$" + totalOrderSum);

					orderComposite.setEnabled(true);
					orderButton.setEnabled(true);
					orderButton.setGrayed(false);

				} else {
					prevDrinkOrderSum = drinksOrderSum;
					totalOrderSum -= drinksOrderSum;
					drinksOrderSum = 0;
					totalOrder.setText("Total:$" + totalOrderSum);
					orderComposite.setEnabled(false);
					if ((!appButtonCheck.getSelection())
							&& (!mainCourseButtonCheck.getSelection())) {
						orderButton.setEnabled(false);
						orderButton.setGrayed(true);

					}
				}
			}
		});

	}

	private String[] getGenres() {
		Genre[] genres = Genre.values();
		String[] res = new String[genres.length];

		for (int i = 0; i < genres.length; i++)
			res[i] = genres[i].toString();

		return res;
	}

	private String[] getCountries() {
		String[] countries = OrderingResources.getCountries();
		return countries;
	}

	private void getOrderDetailsFromControls() {

		orderDetails = null;

		String error = varifyUserInput_orderMovie();

		if (!error.equals("")) {
			userMessage(shell, "Not A vald Input", error);
			return;
		}

		orderDetails = new OrderDetails();

		if (appButtonCheck.getSelection()) {

			AppetizerOrderDetails appetizerOrder = new AppetizerOrderDetails(
					appPrice.getSelection());
			appetizerOrder.setDecade(getDecade());
			orderDetails.setAppetizerOrderDetails(appetizerOrder);
		}

		if (mainCourseButtonCheck.getSelection()) {

			MainCourseOrderDetails mainCourseOrder = new MainCourseOrderDetails(
					mainCoursePrice.getSelection());
			if (byActor.getSelection()) {
				mainCourseOrder.setActor(selectedActorForMainCourse.getText());
				orderDetails.setMainCourseOrderDetails(mainCourseOrder);
			}

			else {
				mainCourseOrder.setActor(null);

				if (FusionButton.getSelection())
					mainCourseOrder.setCountry(null);
				else
					mainCourseOrder.setCountry(countriesCombo.getText());

				Genre selectedGenre = Genre.valueOf(genresCombo.getText());
				mainCourseOrder.setGenre(selectedGenre);
				orderDetails.setMainCourseOrderDetails(mainCourseOrder);

			}
		}

		if (drinksButtonCheck.getSelection()) {

			DrinksOrderDetails drinksOrder = new DrinksOrderDetails(
					drinksPrice.getSelection());
			drinksOrder.setRating(getRating());
			orderDetails.setDrinksOrderDetails(drinksOrder);
		}

		orderDetails.setDessert(true);
	}

	static void userMessage(Shell father, String Title, String error) {
		if (!father.isDisposed()) {
			MessageBox mb = new MessageBox(father, SWT.NONE | SWT.OK);
			mb.setText(Title);
			mb.setMessage(error);
			mb.open();

		}
	}

	private String varifyUserInput_orderMovie() {
		String error = "";
		error += varifyAppetizer();
		error += varifyMainCourse();
		error += varifyDrinks();

		return error;
	}

	private String varifyDrinks() {
		String res = "";

		if (!drinksButtonCheck.getSelection())
			return res;

		if (!alcoholButton.getSelection() && !softDrinksButton.getSelection()
				&& !cofeeButton.getSelection())
			res += "Drinks:\t\tPlease select a drink.\n";

		if (drinksPrice.getSelection() == 0)
			res += "Drinks:\t\tPlease select a price.\n";

		return res;

	}

	private String varifyMainCourse() {

		String res = "";

		// if not selected its all good
		if (!mainCourseButtonCheck.getSelection())
			return res;

		if (byActor.getSelection()) {

			if (selectedActorForMainCourse.getText().equals("Selected Spice")) {
				res += "Main Course:\tPlease select your favorite spice.\n";
			}

			return res;
		}

		if (mainCoursePrice.getSelection() == 0)
			res += "Main Course:\tPlease select a price.\n";

		if (!FusionButton.getSelection()) {
			if (countriesCombo.getText().equals("Select your favorite cuisine")) {
				res += "Main Course:\tPlease select\\write your favorite cuisine.\n";
			}
		}

		if (!Genre.contains(genresCombo.getText())) {
			res += "Main Course:\tPlease select your favorite type.\n";

		}

		return res;

	}

	private String varifyAppetizer() {

		String res = "";

		// if not selected its all good
		if (!appButtonCheck.getSelection())
			return res;

		if (appPrice.getSelection() == 0)
			res += "Appetizers:\tPlease select a price.\n";

		if (!firstDecadeRadio.getSelection()
				&& !secondDecadeRadio.getSelection()
				&& !thirdDecadeRadio.getSelection()) {
			res += "Appetizers:\tPlease select a decade.\n";

		}

		return res;
	}

	private Rating getRating() {
		if (alcoholButton.getSelection())
			return Rating.ADULTS;

		if (softDrinksButton.getSelection())
			return Rating.KIDS;
		else
			return Rating.TEENS;
	}

	private Decade getDecade() {
		if (firstDecadeRadio.getSelection())
			return Decade.OLD;

		if (secondDecadeRadio.getSelection())
			return Decade.MID;
		else
			return Decade.NEW;
	}

	@Override
	public void updateBegin() {

		SWTUtil.getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {

				// progress bar has a good size now
				SetMessage("Update started.");
				subShell = SWTUtil.getShell(SWT.TITLE);
				subShell.setBackgroundImage(mainBackground);
				subShell.setBackgroundMode(SWT.INHERIT_DEFAULT);
				subShell.setText("Update progress");
				subShell.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print",
						8, SWT.BOLD));
				subShell.setLayout(new GridLayout(1, false));
				subShell.setSize(500, 150);

				Composite progressBarComposite = new Composite(subShell,
						SWT.NONE);
				progressBarComposite.setLayout(new FillLayout());
				progressBarComposite.setLayoutData(new GridData(SWT.FILL,
						SWT.NONE, false, false));

				bar = new ProgressBar(progressBarComposite, SWT.SMOOTH);

				updateStatus = new Label(subShell, SWT.NONE);
				updateStatus.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
						true, false));
				updateStatus.setFont(new Font(SWTUtil.getDisplay(),
						"Segoe Print", 8, SWT.BOLD));

				closePBshell = new Button(subShell, SWT.PUSH);
				closePBshell.setText("  Close  ");
				closePBshell.setFont(new Font(SWTUtil.getDisplay(),
						"Segoe Print", 8, SWT.BOLD));
				closePBshell.setEnabled(false);
				closePBshell.setGrayed(true);
				closePBshell.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM,
						true, true));

				closePBshell.addSelectionListener(new SelectionAdapter() {

					@Override
					public void widgetSelected(SelectionEvent e) {

						subShell.close();

					}
				});

				updateStatus.setText("Status:Update started.");

				subShell.addListener(SWT.Close, new Listener() {

					@Override
					public void handleEvent(Event event) {

						event.doit=true;

						shell.setEnabled(true);
						shell.forceActive();

						subShell.dispose();
					}
				});

				shell.setEnabled(false);
				subShell.open();

			}
		});

	}

	@Override
	public synchronized void SetError(final IError error) {

		SWTUtil.getDisplay().syncExec(new Runnable() {

			@Override
			public void run() {
				if (shell !=null && !shell.isDisposed()) {
					if (error != null) {
						if (subShell != null && !subShell.isDisposed()) {
							userMessage(subShell, "Error", error.getReason());
							subShell.close();
						} 
						else
							userMessage(shell, "Error", error.getReason());

						shell.setEnabled(true);
						shell.forceActive();
						return;
					} 
					else{
						if (subShell != null && !subShell.isDisposed()) {
							userMessage(subShell, "Error", "UnKnown Reason");
							subShell.close();
						}

						else
							userMessage(shell, "Error", "UnKnown Reason");

						shell.setEnabled(true);
						shell.forceActive();
						return;
					}
				}
				else{
					shell = SWTUtil.getShell();
					if (error!=null)
						userMessage(shell, "Error", error.getReason());
					else
						userMessage(shell, "Error", "UnKnown Reason");

				}

			}});

	}

	@Override
	public void SetKernel(IKernel kernel) {
		this.ker = kernel;
	}

	@Override
	public IOrderDetails GetOrderDetails() {
		return orderDetails;
	}

	@Override
	public void SetQueryResult(final IQueryResult result) {

		SWTUtil.getDisplay().syncExec(new Runnable() {

			@Override
			public void run() {


				switch (result.getDishType()) {
				case APPETIZER:

					appQuerySignal=true;

					if(appQuerySignal && MCQuerySignal && drinkQuerySignal && DesertQuerySignal){
						backButton.setEnabled(true);
						backButton.setGrayed(false);
					}

					appResultList = result.getResults();

					Collections.sort(appResultList,
							Pair.starsHighFirstComperator);

					appCombo.setItems(Pair.pairListToStringArray(appResultList));

					appCombo.setText("Select your Appetizer");

					if (appCombo.getItemCount() > 0) {						
						appCombo.setEnabled(true);
					} else {

						userMessage(subShell, "No results",
								"Sorry, but the search could not find appetizers.");

						appCombo.setEnabled(true);
						appCombo.setText("no results..:(");
						appCombo.setEnabled(false);
					}

					break;

				case MAINCOURSE:

					MCQuerySignal=true;

					MCResultList = result.getResults();

					Collections.sort(MCResultList, Pair.starsHighFirstComperator);

					McCombo.setItems(Pair.pairListToStringArray(MCResultList));

					McCombo.setText("Select your Main Course");

					if (McCombo.getItemCount() > 0) {
						McCombo.setEnabled(true);
					} else {
						userMessage(subShell, "No results",
								"Sorry, but the search could not find main course.");

						McCombo.setEnabled(true);
						McCombo.setText("no results..:(");
						McCombo.setEnabled(false);
					}
					break;

				case DESSERT:

					DesertQuerySignal=true;

					desertResultList = result.getResults();

					Collections.sort(desertResultList,
							Pair.starsHighFirstComperator);

					desCombo.setItems(Pair.pairListToStringArray(desertResultList));
					desCombo.setEnabled(true);
					desCombo.setText("Select your Desert");
					desCombo.setEnabled(false);

					if (desCombo.getItemCount() > 0) {

					} 
					else {
						userMessage(subShell, "No results",
								"Sorry, but the search could not find desserts.");

						desCombo.setEnabled(true);
						desCombo.setText("no results..:(");
						desCombo.setEnabled(false);
					}
					break;

				case DRINKS:

					drinkQuerySignal=true;

					drinkResultList = result.getResults();

					Collections.sort(drinkResultList,Pair.starsHighFirstComperator);

					drinkCombo.setItems(Pair.pairListToStringArray(drinkResultList));

					drinkCombo.setText("Select your Drink");

					if (drinkCombo.getItemCount() > 0) {
						drinkCombo.setEnabled(true);
					} else {
						userMessage(subShell, "No results",
								"Sorry, but the search could not find drinks.");

						drinkCombo.setEnabled(true);
						drinkCombo.setText("no results..:(");
						drinkCombo.setEnabled(false);
					}
					break;
				}

				if(appQuerySignal && MCQuerySignal && drinkQuerySignal && DesertQuerySignal){
					backButton.setEnabled(true);
					backButton.setGrayed(false);
				}
			}
		});
	}

	@Override
	public synchronized void SetMessage(final String message) {
		if (!shell.isDisposed()) {
			SWTUtil.getDisplay().syncExec(new Runnable() {

				@Override
				public void run() {

					if (message != null) {
						if (logMessage.getText().equals(""))
							logMessage.append(message);
						else
							logMessage.append("\n" + message);
					}

				}
			});

		}

	}

	@Override
	public void SetProgress(final IProgress progress) {

		SWTUtil.getDisplay().syncExec(new Runnable() {

			@Override
			public void run() {

				if (progress != null) {
					bar.setSelection(progress.getProgress());
					if (progress.getState() != null)
						updateStatus.setText("Status: " + progress.getState());
					if (progress.getProgress() == 100)
						closePBshell.setEnabled(true);
				}
			}
		});
	}

	@Override
	public MovieDB getMovieToEdit() {
		return movieToFind_forEdit;

	}

	@Override
	public void setMoviesFoundForEdit(final Map<Integer, MovieDB> moviesFound) {
		if (!shell.isDisposed()) {
			SWTUtil.getDisplay().syncExec(new Runnable() {

				@Override
				public void run() {

					editMovie_stepOne.setEnabled(true);
					findMoviesButton.setEnabled(true);
					findMoviesButton.setGrayed(false);

					if (moviesFound == null) {
						isSearchingArray[MOVIES_FOR_EDIT] = false;
						return;
					}

					if (moviesFound.size() == 0) {
						userMessage(shell, "No results",
								"Sorry, Search could not find movies");
						isSearchingArray[MOVIES_FOR_EDIT] = false;
						return;
					}

					moviesFoundForEdit = moviesFound;
					MovieDB[] moviesFoundArray = mapValuesToMovieDBArray(moviesFound);
					buildMoviesMap(moviesFoundArray);
					// Arrays.sort(moviesForEditNames);
					moviesFoundCombo.setItems(moviesForEditNames);
					editMovie_stepTwo.setEnabled(true);
					moviesFoundCombo.setEnabled(true);

					selectMovieButton.setEnabled(true);
					selectMovieButton.setGrayed(false);
					moviesFoundCombo.setText("Select A movie");
					isSearchingArray[MOVIES_FOR_EDIT] = false;
				}
			});
		}
	}

	private MovieDB[] mapValuesToMovieDBArray(Map<Integer, MovieDB> moviesMap) {
		int size = moviesMap.size();
		MovieDB[] movies = moviesMap.values().toArray(new MovieDB[size]);
		Arrays.sort(movies, MovieDB.movieDBComparator);
		// String[] moviesNames = new String[movies.length];
		//
		// for (int i = 0;i < moviesNames.length;i++)
		// moviesNames[i]=movies[i].getImdbName();

		return movies;
	}

	private void buildMoviesMap(MovieDB[] moviesFound) {

		if (moviesFound != null) {
			moviesForEdit = new HashMap<String, MovieDB>();
			int min = Math.min(MAX_NUMBER_OF_RESULTS, moviesFound.length);

			if (min == MAX_NUMBER_OF_RESULTS)
				SetMessage("Too many movies were found. showing only the first "
						+ MAX_NUMBER_OF_RESULTS);

			moviesForEditNames = new String[min];
			for (int i = 0; i < moviesForEditNames.length; i++) {
				moviesForEdit.put(moviesFound[i].getImdbName(), moviesFound[i]);
				moviesForEditNames[i] = moviesFound[i].getImdbName();
			}
		}
	}

	@Override
	public void setActorsFoundForEdit(final Map<Integer, String> actorsFound) {
		if (!shell.isDisposed()) {
			SWTUtil.getDisplay().syncExec(new Runnable() {

				@Override
				public void run() {

					editActor_stepOne.setEnabled(true);
					findActorButton.setEnabled(true);
					findActorButton.setGrayed(false);

					if (actorsFound == null) {
						isSearchingArray[ACTORS_FOR_EDIT] = false;
						return;
					}

					if (actorsFound.size() == 0) {
						userMessage(shell, "No results",
								"Sorry, Search could not find actors");
						isSearchingArray[ACTORS_FOR_EDIT] = false;
						return;
					}
					actorsFoundForEdit = actorsFound;
					actorsForEditNames = mapValuesToActorsNamesArray(actorsFound);
					Arrays.sort(actorsForEditNames);
					actorsFoundCombo.setItems(actorsForEditNames);
					editActor_stepTwo.setEnabled(true);
					actorsFoundCombo.setEnabled(true);
					selectActorButton.setEnabled(true);
					selectActorButton.setGrayed(false);
					actorsFoundCombo.setText("Select An Actor");
					isSearchingArray[ACTORS_FOR_EDIT] = false;
				}
			});
		}
	}

	private String[] mapValuesToActorsNamesArray(
			Map<Integer, String> actorsFound) {
		int size = actorsFound.size();
		String[] actorsSorted = actorsFound.values().toArray(new String[size]);

		Arrays.sort(actorsSorted);

		if (actorsSorted.length > MAX_NUMBER_OF_RESULTS) {

			SetMessage("Too many actors were found. Showing only the first "
					+ MAX_NUMBER_OF_RESULTS);

			String[] actors = new String[MAX_NUMBER_OF_RESULTS];
			for (int i = 0; i < MAX_NUMBER_OF_RESULTS; i++)
				actors[i] = actorsSorted[i];
			return actors;
		}

		return actorsSorted;
	}

	@Override
	public TVshowDB getTvShowToEdit() {
		return tvShowToFind_forEdit;
	}

	@Override
	public void setTvShowsFoundForEdit(final Map<Integer, TVshowDB> tvShowsFound) {
		if (!shell.isDisposed()) {
			SWTUtil.getDisplay().syncExec(new Runnable() {

				@Override
				public void run() {

					editTvShow_stepOne.setEnabled(true);
					findTvShowButton.setEnabled(true);
					findTvShowButton.setGrayed(false);

					if (tvShowsFound == null) {

						isSearchingArray[TV_FOR_EDIT] = false;
						return;
					}

					if (tvShowsFound.size() == 0) {
						userMessage(shell, "No results",
								"Sorry, Search could not find TV Show");
						isSearchingArray[TV_FOR_EDIT] = false;
						return;
					}

					tvShowsFoundForEdit = tvShowsFound;
					TVshowDB[] tvShowsFoundArray = mapValuesToTVshowDBArray(tvShowsFound);
					buildTvShowsMap(tvShowsFoundArray);
					Arrays.sort(tvShowsForEditNames);
					tvShowsFoundCombo.setItems(tvShowsForEditNames);
					editTvShow_stepTwo.setEnabled(true);
					tvShowsFoundCombo.setEnabled(true);
					selectTvShowButton.setEnabled(true);
					selectTvShowButton.setGrayed(false);
					tvShowsFoundCombo.setText("Select A TV Show");
					isSearchingArray[TV_FOR_EDIT] = false;
				}
			});

		}
	}

	private void buildTvShowsMap(TVshowDB[] tvShowsFoundArray) {

		if (tvShowsFoundArray != null) {
			tvShowsForEdit = new HashMap<String, TVshowDB>();
			tvShowsForEditNames = new String[tvShowsFoundArray.length];
			for (int i = 0; i < tvShowsFoundArray.length; i++) {
				tvShowsForEdit.put(tvShowsFoundArray[i].getImdbName(),
						tvShowsFoundArray[i]);
				tvShowsForEditNames[i] = tvShowsFoundArray[i].getImdbName();
			}
		}

	}

	private TVshowDB[] mapValuesToTVshowDBArray(
			Map<Integer, TVshowDB> tvShowsFound) {
		int size = tvShowsFound.size();
		TVshowDB[] shows = tvShowsFound.values().toArray(new TVshowDB[size]);

		return shows;
	}

	@Override
	public Integer getSelectedMovieID() {
		return selectedMovie_id;
	}

	@Override
	public MovieDB getEditedMovie() {
		return editedMovie;
	}

	@Override
	public actorDB getNewActor() {
		return newActor;
	}

	@Override
	public Integer getSelectedActorID() {
		return selectedActor_id;
	}

	@Override
	public MovieDB getNewMovie() {

		return newMovie;
	}

	@Override
	public TVshowDB getNewTvShow() {

		return newTvShow;
	}

	@Override
	public MovieLink getActorMovieLink() {
		return newMovieLink;
	}

	@Override
	public String getEditedActor() {
		return editedActor;
	}

	@Override
	public Integer getSelectedTvShowID() {
		return selectedTvShow_id;
	}

	@Override
	public TVshowDB getEditedTvShow() {
		return editedTvShow;
	}

	@Override
	public MovieDB getMovieToAdd() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public actorDB getActorToEdit() {
		return actorToFind_forEdit;
	}

	@Override
	public actorDB getActorParametersForAddConnection() {
		return actorToFind_forAddConnection;
	}

	@Override
	public void setActorsFoundForAddConnection(
			final Map<Integer, String> actorsFound) {

		if (!shell.isDisposed()) {
			SWTUtil.getDisplay().syncExec(new Runnable() {

				@Override
				public void run() {

					findActorForAddConnectionButton.setEnabled(true);
					findActorForAddConnectionButton.setGrayed(false);

					if (actorsFound == null) {
						isSearchingArray[ACTORS_FOR_ADDCON] = false;
						return;
					}

					if (actorsFound.size() == 0) {
						userMessage(shell, "No results",
								"Sorry, Search could not find actors");
						isSearchingArray[ACTORS_FOR_ADDCON] = false;
						return;
					}
					actorsFoundForAddConnection = actorsFound;
					actorsForAddConnectionNames = mapValuesToActorsNamesArray(actorsFound);
					// Arrays.sort(actorsForAddConnectionNames);
					actorsFoundForAddConnectionCombo
					.setItems(actorsForAddConnectionNames);
					selectActorForAddConnectionComposite.setEnabled(true);
					actorsFoundForAddConnectionCombo.setEnabled(true);
					actorsFoundForAddConnectionCombo
					.setText("Select An Actor                     ");
					isSearchingArray[ACTORS_FOR_ADDCON] = false;
				}
			});

		}
	}

	@Override
	public int getMovieIDForAddConnection() {

		return movieIDforAddConnection;

	}

	@Override
	public int getActorIDForAddConnection() {

		return actorIDForAddConnection;

	}

	@Override
	public void setMoviesFoundForAddConnection(
			final Map<Integer, MovieDB> moviesFound) {
		if (!shell.isDisposed()) {
			SWTUtil.getDisplay().syncExec(new Runnable() {

				@Override
				public void run() {

					findMovieForAddConnectionButton.setEnabled(true);
					findMovieForAddConnectionButton.setGrayed(false);

					if (moviesFound == null) {

						isSearchingArray[MOVIES_FOR_ADDCON] = false;
						return;

					}

					if (moviesFound.size() == 0) {
						userMessage(shell, "No results",
								"Sorry, Search could not find movies");

						isSearchingArray[MOVIES_FOR_ADDCON] = false;
						return;
					}

					editMovie_stepOne.setEnabled(true);
					findMoviesButton.setEnabled(true);
					findMoviesButton.setGrayed(false);

					moviesFoundForAddConnection = moviesFound;
					MovieDB[] moviesFoundArray = mapValuesToMovieDBArray(moviesFound);
					buildMoviesForAddConnectionMap(moviesFoundArray);
					// Arrays.sort(moviesForAddConnectionNames);
					moviesFoundForAddConnectionCombo
					.setItems(moviesForAddConnectionNames);
					selectMovieForAddConnectionComposite.setEnabled(true);
					moviesFoundForAddConnectionCombo.setEnabled(true);
					moviesFoundForAddConnectionCombo
					.setText("Select A movie                      ");

					isSearchingArray[MOVIES_FOR_ADDCON] = false;

				}
			});
		}
	}

	private void buildMoviesForAddConnectionMap(MovieDB[] moviesFound) {

		if (moviesFound != null) {
			moviesForAddConnection = new HashMap<String, MovieDB>();
			int min = Math.min(MAX_NUMBER_OF_RESULTS, moviesFound.length);

			if (min == MAX_NUMBER_OF_RESULTS)
				SetMessage("Too many movies were found. showing only the first "
						+ MAX_NUMBER_OF_RESULTS);
			moviesForAddConnectionNames = new String[min];
			for (int i = 0; i < moviesForAddConnectionNames.length; i++) {
				moviesForAddConnection.put(moviesFound[i].getImdbName(),
						moviesFound[i]);
				if (moviesFound[i].getImdbName() != null)
					moviesForAddConnectionNames[i] = moviesFound[i]
							.getImdbName();
				else
					moviesForAddConnectionNames[i] = "THERE_IS_NO_NAME";
			}
		}

	}

	@Override
	public MovieDB getMovieParametersForAddConnection() {
		return movieToFind_forAddConnection;
	}

	@Override
	public actorDB getActorParametersForMainCourse() {
		final actorDB actor = new actorDB();
		SWTUtil.getDisplay().syncExec(new Runnable() {

			@Override
			public void run() {
				findActorForMainCourse.getActorParams(actor);
			}
		});

		return actor;
	}

	@Override
	public void setActorsFoundForMainCourse(
			final Map<Integer, String> actorsFound) {
		if (actorsFound != null) {
			String[] actors = mapValuesToActorsNamesArray(actorsFound);
			findActorForMainCourse.setActorsFound(actors);
		}

		else
			findActorForMainCourse.setActorsFound(null);

	}

	@Override
	public String getDBFilesPath() {
		return filesPath;
	}

	@Override
	public void close() {
		shell.dispose();

	}

}
